<?php

namespace App\Http\Controllers\admin;
use DB;
use Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\recommend\addRequests;
use App\Http\Requests\recommend\EditRequest;
use App\Http\Controllers\Controller;

class RecommendController extends Controller
{
    /**
     *推荐模块添加
     * /admin/recommend/add
     * /admin/recommend/insert
     */
    //编辑添加
    public function getAdd() {

        //跳转到添加页面 
        return view("recommend.add");
    }

    //执行添加
    public function postInsert(addRequests $request) {

        //数据处理
        //将倒计时转化为秒数
        $t = $request -> input("h") * 60 * 60 + $request -> input("i") * 60 + $request -> input("s") + $request -> input("d") * 24 * 3600;
        
        //图片信息处理
        if ($request -> hasFile("pic")) {
            $path = config::get("app.upload_path"); //获取文件上传的路径
            
            //给图片重命名
            do {
                $picname = date("YmdHis").rand(10000,9999).$request->file('pic')->getClientOriginalName();
            }while(file_exists($path.$picname));
        }

        //过滤多余字段
        $data = $request -> except(["_token", "d", "h", "i", "s" ,"pic"]);

        //注入处理后的字段
        $data["pic"] = $picname;
        $data["time"] = $t;
        // dd($data);
        //执行插入
        $res = DB::table("gs_recommend") -> insert($data);

        //解析结果集
        if ($res) {
            //成功则执行图片上传和缩放
            //执行上传  
            $request->file('pic') -> move($path,$picname); 

            //设置图片缩放必要参数
            $imgDir = $path.$picname;

            //缩放图片
            imgR($imgDir,156,150,"m_");
            imgR($imgDir,50,50,"s_");

            //页面跳转并提示
            return redirect("/admin/recommend/index") -> with("success", "添加成功！");
        } else {

            //失败返回并提示
            return back() -> with("error", "添加失败！");
        }
    }

    /**
     * 推荐模块浏览
     * /admin/recommend/index
     */
    public function getIndex(Request $request) {
        
        //获取图片上传路径
        $path = trim(config::get("app.upload_path"),".");

        //查询数据
        $res = DB::table("gs_recommend") -> where(function($query)use($request) {
            
            //判断是否存在搜索
            if ($request -> input("keywords")) {
                $query -> where("order", $request -> input("keywords"))
                -> orWhere("id", $request -> input("keywords"))
                -> orWhere("name", "like", "%".$request -> input("keywords")."%");
            } 
        }) -> paginate($request -> input("num",10));
        
        //返回到浏览界面
        return view("recommend.index", ["list" => $res, "path" => $path, "request" => $request -> all()]);
    }

    /**
    *banner是否前台显示修改
    * /admin/banner/delete  浏览页面
    */
    public function getStatus($status,$id) {
        
        //根据当前状态确定修改过后的状态
        if($status == 1) {
            $num = 0;
        } else {
            $num = 1;
        }
        
        //执行修改
        $res = DB::table("gs_recommend") -> where("id", $id) -> update(["status" => $num]);

        //解析结果集
        if ($res){

            //返回并提示
            return redirect("/admin/recommend/index") -> with("success", "修改成功！");
        } else {
            return back() -> with("error", "修改失败！");
        }
    }

    /**
    *banner删除
    * /admin/banner/delete  浏览页面
    */
    public function getDelete($id,$pic) {
        
        //删除该模板下的商品列表
        $res1 = DB::table("rec_gs") -> where("tid", "$id") -> delete();

        //解析结果集
        if ($res1) {    //删除成功,继续删除版块
            
            //获取图片上传路径
            $path = config::get("app.upload_path");
            
            //删除数据库内容
            $res = DB::table("gs_recommend") -> where("id", $id) -> delete();

            //解析结果集
            if ($res) {

                //成功则删除图片
                unlink($path.$pic);
                unlink($path."s_".$pic);
                unlink($path."m_".$pic);


                //跳转并告知用户
                return redirect("/admin/recommend/index") -> with("success", "删除成功！");
            } else {

                //失败则返回
                return back() -> with("error", "删除失败！");
            }
        } else {    //删除失败返回并提示
            return back() -> with("error", "删除失败！");
        }
    }
    
    /**
    *修改
    * /admin/recommend/edit  编辑页面
    * /admin/recommend/update 执行修改
    */
    public function getEdit($id) {

        //获取图片上传路径
        $path = ltrim(config::get("app.upload_path"),".");
        
        //查询原有信息
        $res = DB::table("gs_recommend") -> where("id",$id) -> first(); 
        
        //跳转到编辑页面
        return view("recommend.edit", ["v" => $res, "path" => $path]);
    }

    //修改
    public function postUpdate(EditRequest $request) {
        
        //处理数据      
        //将倒计时转化为秒数
        $t = $request -> input("h") * 60 * 60 + $request -> input("i") * 60 + $request -> input("s") + $request -> input("d") * 24 * 3600;
        
        //过滤多余字段
        $data = $request -> except(["_token", "d", "h", "i", "s" ,"pic","opic"]);
        
        //图片信息处理
        if ($request -> hasFile("pic")) {
            $path = config::get("app.upload_path"); //获取文件上传的路径
            
            //给图片重命名
            do {
                $picname = date("YmdHis").rand(10000,9999).$request->file('pic')->getClientOriginalName();
            }while(file_exists($path.$picname));

            //注入处理后的字段
            $data["pic"] = $picname;
        }       
        $data["time"] = $t;
        
        //修改数据库
        $res = DB::table("gs_recommend") -> where("id",$request -> input("id")) -> update($data);
        
        //解析结果集
        if ($res){      //成功
            
            //如果存在图片更改
            if (isset($data["pic"])) {
            
                //执行图片上传和缩放
                $path = config::get("app.upload_path"); //获取文件上传的路径

                //执行上传  
                $request->file('pic') -> move($path,$picname); 

                //设置图片缩放必要参数
                $imgDir = $path.$picname;

                //缩放图片
                imgR($imgDir,156,150,"m_");
                imgR($imgDir,50,50,"s_");

                //删除旧图
                unlink($path.$request -> input("opic"));
                unlink($path."s_".$request -> input("opic"));
                unlink($path."m_".$request -> input("opic"));

            }
            //返回并提示
            return redirect("/admin/recommend/index") -> with("success", "修改成功！");
        } else {    //失败
            return back() -> with("error", "修改失败！");
        }
    }

     /**
    *商品添加
    * /admin/recommend/addgs  添加页面
    * /admin/recommend/gsinsert 执行添加
    */
    public function getAddgs( $id, $tname, Request $request) {

        //查找该版块下已经添加的商品ID
        $res1 = DB::table("rec_gs") -> select("gid") -> where("tid", $id) -> get();
        
        //拼装查询排除的字段
        $arr = [];
        foreach ($res1 as $v) {
            $arr[] = $v["gid"];
        }
        
        //获取图片上传路径
        $path = trim(config::get("app.upload_path"),".");

        //数据查询
        $res = DB::table("goods") -> select("id", "name", "on_sale", "pic", "scan_count", "sell_count", "love_count", "ctime")
            -> whereNotIn("id", $arr)
            -> where(function($query)use($request) {
            
            //判断是否存在搜索
            if ($request -> input("keywords")) {
                $query -> where("id", $request -> input("keywords"))
                -> orWhere("name", "like", "%".$request -> input("keywords")."%");
            } 
        })  -> paginate($request -> input("num",10));

        //跳转到插入界面
        return view("recommend.addgs", ["list" => $res, "path" => $path, "request" => $request -> all(), "id" => $id, "tname" => $tname]);
    }

    //执行添加
    public function getGsinsert($gid, $tid, $tname) {

        //插入关联表
        $res = DB::table("rec_gs") -> insert(['gid' => $gid, 'tid' => $tid]);
        
        //解析结果集
        if ($res) {

            //成功返回并提示
            return redirect("/admin/recommend/list/".$tid."/".$tname) -> with("success", "添加成功！");
        } else {
            
            //失败返回并提示
            return back() -> with("error", "添加失败！");
        }
    }

    /**
     * 版块下商品的浏览
     * /admin/recommend/list  浏览页面
     */
    public function getList(Request $request, $id, $tname) {

        //查找该版块下已经添加的商品ID
        $res1 = DB::table("rec_gs") -> select("gid") -> where("tid", $id) -> get();
        
        //拼装查询排除的字段
        $arr = [];
        foreach ($res1 as $v) {
            $arr[] = $v["gid"];
        }
        
        //获取图片上传路径
        $path = trim(config::get("app.upload_path"),".");

        //数据查询
        $res = DB::table("goods") -> select("id", "name", "on_sale", "pic", "scan_count", "sell_count", "love_count", "ctime")
            -> whereIn("id", $arr)
            -> where(function($query)use($request) {
            
            //判断是否存在搜索
            if ($request -> input("keywords")) {
                $query -> where("id", $request -> input("keywords"))
                -> orWhere("name", "like", "%".$request -> input("keywords")."%");
            } 
        })  -> paginate($request -> input("num",10));

        //跳转到插入界面
        return view("recommend.list", ["list" => $res, "path" => $path, "request" => $request -> all(), "id" => $id, "tname" => $tname]);
    }

    /**
     * 版块下商品的删除
     * /admin/recommend/gsdelete 浏览页面
     */
    public function getGsdelete($gid, $tid) {
        
        //执行删除
        $res = DB::table("rec_gs") -> where("tid", $tid) -> where("gid", $gid) -> delete();
        
        //解析结果集
        if($res) {

            //成功则返回并提示
            return back() -> with("success", "删除成功！");
        } else {

            //失败返回并提示
            return back() -> with("error", "删除失败！");
        }
    }
}
